package com.example.shiwu.service;

import android.content.Context;
import com.example.shiwu.db.dao.DeviceDao;
import com.example.shiwu.db.dao.DeviceVoDao;
import com.example.shiwu.db.dao.base.BaseDaoFactory;
import com.example.shiwu.db.model.Device;
import com.example.shiwu.db.vo.DeviceVo;
import com.example.shiwu.manager.DatabaseHelper;

import java.util.ArrayList;
import java.util.List;

public class DeviceService {

    private DatabaseHelper dbHelper;
    private DeviceDao<Device> deviceDao;
    private DeviceVoDao<DeviceVo> deviceVoDao;
    private ArrayList<Device> listDevice = null;
    private ArrayList<DeviceVo> listDeviceVo = null;

    private static final int PAGE_SIZE = 10;
    private static final int TOTAL_PAGE = 0;

    public DeviceService() {
    }

    public DeviceService(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    public List<DeviceVo> getAll(Integer currentPage,Integer pageSize){
        deviceVoDao = BaseDaoFactory.getInstance().createBaseDao(DeviceVoDao.class, DeviceVo.class);
        DeviceVo device = new DeviceVo();
        List<DeviceVo> lis = deviceVoDao.query(device,"tb_device dev,tb_items ite",
                new String[]{"ite.id","ite.name","ite.location","ite.num","ite.customer_name","ite.construction_unit","ite.operator","ite.create_time","dev.device_type"},
                "",null,"ite.id","ite.create_time","",currentPage,pageSize);
        return lis;
    }

    /**获取单个数据
     * @param device
     * @return
     */
    public List<Device> getDevice(Device device) {
        deviceDao = BaseDaoFactory.getInstance().createBaseDao(DeviceDao.class, Device.class);
        listDevice = new ArrayList<>();
        listDevice.addAll(deviceDao.query(device));
        return listDevice;
    }

    /**分页获取数据
     * @param device
     * @return
     */
    public List<Device> getDeviceList(Device device,Integer startIndex,Integer limit) {
        deviceDao = BaseDaoFactory.getInstance().createBaseDao(DeviceDao.class, Device.class);
        listDevice = new ArrayList<>();
        listDevice.addAll(deviceDao.query(device,null,"create_time desc",null,startIndex,limit));
        return listDevice;
    }

    public List<DeviceVo> getDeviceVoList(DeviceVo device,Integer startIndex,Integer limit) {
        deviceDao = BaseDaoFactory.getInstance().createBaseDao(DeviceDao.class, Device.class);
        listDeviceVo = new ArrayList<>();
        listDeviceVo.addAll(deviceVoDao.query(device,null,"create_time desc",null,startIndex,limit));
        return listDeviceVo;
    }

    public boolean save(Device device) {
        deviceDao = BaseDaoFactory.getInstance().createBaseDao(DeviceDao.class, Device.class);
        long size = deviceDao.insert(device);
        if(size >0 ){
            return true;
        }
        return false;
    }
    public boolean saveOrUpdata(Device device) {
        deviceDao = BaseDaoFactory.getInstance().createBaseDao(DeviceDao.class, Device.class);
        List<Device> lis =deviceDao.query(device);
        Boolean bl = true;
        if(lis.size()>0){
            long size = deviceDao.update(device,new Device());
            if(size >0 ){
                return bl;
            }
            bl = false;
        }else{
            long size = deviceDao.insert(device);
            if(size >0 ){
                return bl;
            }
            bl = false;
        }

        return bl;
    }

    public boolean updata(Device device,Device where) {
        long size = deviceDao.update(device,where);
        if(size >0 ){
            return true;
        }
        return false;
    }


}
